export function createRouteMap(routes, map = {}) {
  let pathMap = map

  routes.forEach(route => {
    addRouteRecord(route, pathMap)
  })
  return {
    pathMap
  }
}

// 构建父子关系， 路径匹配时 根据路径找到对应的组件
export function addRouteRecord(route, pathMap, parent) {
  let path = parent ? (parent.path + '/' + route.path) : route.path
  let record = {
    name: route.name,
    path: route.path, //  ?????  path
    component: route.component,
    parent
  }
  if (!pathMap[path]) {
    // 防止同一个路径多次注册
    pathMap[path] = record
  }

  if (route.children) {
    route.children.forEach(child => {
      addRouteRecord(child, pathMap, record)
    })
  }
}